package com.zyd.shiro.controller.admin;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageInfo;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.util.QiniuUtil;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import com.zyd.shiro.framework.object.PageResult;
import org.springframework.web.bind.annotation.*;

import com.zyd.shiro.persistence.beans.Category;
import com.zyd.shiro.business.service.MallCategoryService;

import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.util.ResultUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 控制器层
 *
 * @author RookieWZW
 */
@RestController
@CrossOrigin
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private MallCategoryService categoryService;

    @Autowired
    private QiniuUtil qiniuUtil;


    /**
     *获取树状分类结构
     * @return
     */
    @GetMapping(value = "asLevel")
    public ResponseVO findCategory() {
        return ResultUtil.success("查询成功", categoryService.findAsMultiLevel());
    }

    /***
     * 获取品类子节点(平级)
     * @return
     */
    @RequestMapping(value = "/getCategory", method = RequestMethod.GET)
    public ResponseVO findCategoryParallel() {

        return ResultUtil.success("查询成功", categoryService.findCategoryParallel(0));
    }

    /***
     * .获取当前分类及递归子节点category
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "/getDeepCategory", method = RequestMethod.GET)
    public ResponseVO findDeepCategory(@RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {

        return ResultUtil.success("查询成功", categoryService.selectCategoryAndChildrenById(categoryId));
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseVO findOne(@PathVariable Integer id) {
        return ResultUtil.success("查询成功", categoryService.findOne(id));

    }

    /**
     * 增加
     *
     * @param category
     */
    @PostMapping(value = "/add")
    public ResponseVO add(Category category, MultipartFile file) {
        categoryService.add(category, file);
        return ResultUtil.success("增加成功");

    }

    /**
     * 修改，照顾图片上传，修改方法为post
     *
     * @param category
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResponseVO update(Category category, MultipartFile file) {
        if (categoryService.update(category, file))
            return ResultUtil.success("修改成功");
        return ResultUtil.error("非法操作！");

    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseVO delete(@PathVariable Integer id) {
        boolean flag = categoryService.delete(id);
        if (flag) {
            return ResultUtil.success("删除成功");
        } else {
            return ResultUtil.error("删除失败");
        }

    }
    @PostMapping(value = "/remove")
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        for (Long id : ids) {
            categoryService.delete(Math.toIntExact(id));
        }

        return ResultUtil.success("成功删除 [" + ids.length + "] 个资源");
    }

    /**
     * 根据分页查看所有分类信息
     *
     * @param vo 查询条件
     * @return 分页信息
     */
    @GetMapping("/pagelist")
    public PageResult getAll(BaseConditionVO vo) {
        PageInfo<Category> pageInfo = categoryService.findCategoryByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    /***
     *
     *前端搜索
     * 跳转到商品列表页
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseVO list() {

        List<Category> categoryList = categoryService.findAll();

        return ResultUtil.success("查询成功", categoryList);
    }


}
